package com.lb.activity.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lb.activity.mapper.ExaminePermissionMapper;
import com.lb.activity.po.ExaminePermission;
import com.lb.activity.po.ExamineRole;
import com.lb.activity.service.ExaminePermissionService;
import com.lb.activity.util.ResultUtil;
import com.lb.activity.util.SnowflakeIdWorker;
import com.lb.activity.util.emumdata.DataEnum;
import com.lb.activity.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author liubin
 */
@Service
public class ExaminePermissionServiceImpl implements ExaminePermissionService {

    @Autowired
    private SnowflakeIdWorker idWorker;

    @Autowired
    private ExaminePermissionMapper examinePermissionMapper;

    @Override
    public ResultUtil list(PermissionVo permissionVo) {
        PageHelper.startPage(permissionVo.getPage(),permissionVo.getLimit());
        Example example = new Example(ExaminePermission.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("status", DataEnum.DELETE_STATUS.getStatus());
        List<ExaminePermission> permissions = examinePermissionMapper.selectByExample(example);
        PageInfo<ExaminePermission> pageInfo = new PageInfo<>(permissions);
        return ResultUtil.build(0, "", Integer.parseInt(String.valueOf(pageInfo.getTotal())), pageInfo.getList());
    }


    @Override
    public ResultUtil add(ExaminePermission permission) {
        permission.setId(idWorker.nextId());
        permission.setStatus(DataEnum.INSERT_STATUS.getStatus());
        // 执行插入
        examinePermissionMapper.insertSelective(permission);
        return ResultUtil.ok();
    }

    @Override
    public ExaminePermission getOne(Long id) {
        return examinePermissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResultUtil update(ExaminePermission permission) {
        examinePermissionMapper.updateByPrimaryKeySelective(permission);
        return ResultUtil.ok();
    }

    @Override
    public ResultUtil delete(Long id) {
        ExaminePermission role = ExaminePermission.builder().id(id).status(DataEnum.DELETE_STATUS.getStatus()).build();
        examinePermissionMapper.updateByPrimaryKeySelective(role);
        return ResultUtil.ok();
    }

    @Override
    public ResultUtil changeStatus(Long id, boolean boolStatus) {
        Long status = DataEnum.INSERT_STATUS.getStatus();
        // 如果为true ,为 lock 即 禁用
        if(boolStatus){
            status = DataEnum.FORBIDDEN_STATUS.getStatus();
        }
        ExaminePermission permission = ExaminePermission.builder().id(id).status(status).build();
        examinePermissionMapper.updateByPrimaryKeySelective(permission);
        return ResultUtil.ok();
    }
}
